﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CML.Site.Models.Demo;
using System.Reflection;
using System.IO;
using CML.Services.Interfaces;
using CML.Data;

namespace CML.Site.Controllers
{
    public class DemoController : Controller
    {
		private readonly ICMLInterpreterService CMLInterpreterService;

		public DemoController(ICMLInterpreterService cmlInterpreterService)
		{
			CMLInterpreterService = cmlInterpreterService;
		}

        public ActionResult Index()
        {
			var m = new IndexModel();
			m.Grammer = GetResource("CML.Grammer.CML.pegjs");
			m.Sample = GetResource("CML.Samples.good.1.cml");
            return View(m);
        }

		public JsonResult RunScript(RunScriptRequest request)
		{
			try
			{
				var result = CMLInterpreterService.RunScript(request);
				return Json(new { 
					success = true,
					result = result
				});
			}
			catch (Exception x)
			{
				return Json(new {success= false, message = x.Message});
			}
		}

		public string GetResource(string resourceName)
		{
			return Resources.Resource.GetResource(resourceName);
		}
    }
}
